package com.zzl.bonds.ticket.controller;

import com.zzl.apidoc.annotation.ApidocController;
import com.zzl.apidoc.annotation.ApidocOperation;
import com.zzl.apidoc.annotation.ApidocParam;
import com.zzl.bonds.ticket.constant.status.CreditStatusEnum;
import com.zzl.bonds.ticket.constant.type.CreditTypeEnum;
import com.zzl.bonds.ticket.dto.KeyValue;
import com.zzl.bonds.ticket.dto.credit.CoreCreditDetail;
import com.zzl.bonds.ticket.dto.credit.CoreEnterpriseCreditPage;
import com.zzl.bonds.ticket.dto.credit.CreditSelectDto;
import com.zzl.bonds.ticket.dto.credit.StatisticCreditDataDto;
import com.zzl.bonds.ticket.service.CoreEnterpriseCreditService;
import com.zzl.general.common.apidoc.annotation.ApidocParamsPage;
import com.zzl.general.common.apidoc.annotation.ApidocResultKeyValR;
import com.zzl.general.common.apidoc.annotation.ApidocReturnPage;
import com.zzl.general.common.apidoc.annotation.ApidocReturnR;
import com.zzl.general.common.apidoc.constants.ResultTypeInR;
import com.zzl.general.common.ctrl.WebBaseCtrl;
import com.zzl.general.common.exception.SysException;
import com.zzl.general.common.utils.LogUtils;
import com.zzl.general.common.utils.PageUtils;
import com.zzl.general.common.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 核心企业授信表
 *
 * @author sunpp
 * @date 2022-02-16 11:14:07
 */
@ApidocController("1核心企业授信表")
@RestController
@SuppressWarnings("javadoc")
@RequestMapping("bondsticket/coreCredit")
@Slf4j
public class CoreEnterpriseCreditController extends WebBaseCtrl {
    @SuppressWarnings("all")
    @Autowired
    private CoreEnterpriseCreditService coreEnterpriseCreditService;

    @GetMapping("/getCreditStatus")
    @ApidocOperation(description = "开立-->额度状态枚举", operationId = "ticket/coreCredit/getCreditStatus")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", genericTypeClass = KeyValue.class)
    })
    public Result getCreditStatus() {
        return Result.ok().put("list", CreditStatusEnum.getKvList());
    }

    @GetMapping("/getCreditScope")
    @ApidocOperation(description = "授信范围枚举", operationId = "ticket/coreCredit/getCreditScope")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", genericTypeClass = KeyValue.class)
    })
    public Result getCreditScope() {
        return Result.ok().put("list", CreditTypeEnum.getKvList(CreditTypeEnum.GROUP.getDescription()));
    }


    /**
     * 分页查询核心企业授信授信
     */
    @GetMapping("/list")
    @ApidocOperation(description = "额度管理-凭证融资额度列表", operationId = "2001")
    @ApidocReturnPage(genericTypeClass = CoreEnterpriseCreditPage.class)
    public Result list(@ApidocParamsPage(name = "params", keyName = {
            "creditNo",
            "enterpriseId",
            "financeInstitutyId",
            "effectiveStartTime",
            "effectiveEndTime",
            "creditStatus",
            "creditType"
    }, keyDescription = {
            "额度编号",
            "授信主体",
            "金融机构",
            "有效期开始日期",
            "有效期结束日期",
            "额度状态，全部时，不传入值",
            "授信范围"
    }
    ) @RequestParam Map<String, Object> params) {
        PageUtils page = null;
        try {
            page = coreEnterpriseCreditService.queryPage(params, this.getUser());
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }
        return Result.ok().put("page", page);
    }

    @GetMapping("/getData")
    @ApidocOperation(description = "三个统计数据", operationId = "ticket/coreCredit/getData")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", genericTypeClass = StatisticCreditDataDto.class)
    })
    public Result getTodayOpenStatistics() {
        return Result.ok().put("data", coreEnterpriseCreditService.getOpenData(this.getUser()));
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApidocOperation(description = "额度详情", operationId = "2002")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "coreEnterpriseCredit", valueClass = CoreCreditDetail.class)
    })
    public Result info(@ApidocParam(name = "id", description = "核心企业授信表id", required = true) @PathVariable("id") String id) {
        try {
            return Result.ok().put("data", coreEnterpriseCreditService.getCoreCreditDetail(id));
        } catch (SysException e) {
            LogUtils.logSysException(log, e);
            return Result.error(e.getMsg());
        }

    }

    @GetMapping("/getSelectCredit/{openEnterpriseId}")
    @ApidocOperation(description = "凭证开具-开单额度下拉框", operationId = "ticket/coreCredit/getSelectCredit")
    @ApidocReturnR(otherkv = {
            @ApidocResultKeyValR(resultType = ResultTypeInR.TYPE_OBJECT, key = "data", genericTypeClass = CreditSelectDto.class)
    })
    public Result getSelectCredit(@ApidocParam(name = "openEnterpriseId", description = "开单企业", required = true) @PathVariable("openEnterpriseId") String openEnterpriseId) {
        return Result.ok().put("list", coreEnterpriseCreditService.getCreditByOpenEnterpriseId(openEnterpriseId));
    }

}
